<template>
  <div class="waterfall-grid">
    <div v-for="(item, index) in postList" :key="index" class="container">
      <div class="image-wrapper">
        <video v-if="item.postPhoto.split('.').pop()==='mp4'" :src="item.postPhoto" controls @click="commentDetails(item)" />
        <img v-else :src="item.postPhoto" alt="图片" @click="commentDetails(item)">
        <span class="title">{{ item.postTitle }}</span>&nbsp;
      </div>
      <div class="user-info">
        <img class="user-icon" :src="item.photo" alt="�û�ͼ��">
        <span>{{ item.name }}</span>
        <div class="checkbox-wrapper">
          <span class="like-count">点赞数:{{ item.postPraise }}</span>
        </div>
      </div>
    </div>
    <blog-detall ref="postDetails" @commentDetails="commentDetails" />
  </div>
</template>
<script>
import { deleteAction, getAction, postAction } from '@/utils/xhs-ajax'
import blogDetall from '@/views/userHome/toUserHome/detall/toMyBlogDetail.vue'
import item from '@/layout/components/Sidebar/Item.vue'

export default {
  components: {
    blogDetall
  },
  data() {
    return {
      userId: this.$route.params.id,
      imageUrl: '',
      label: [],
      checkLabel: [], // 选中的标签
      theitem: {
        postId: '',
        userId: '',
        postTitle: '',
        postContent: '',
        postPhoto: '',
        labelList: []
      },
      dialogVisible: false,
      postList: [],
      heartPath: 'M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z' // ����ͼ���·������
    }
  },
  computed: {
    item() {
      return item
    },
    displayedImage() {
      return this.imageUrl || this.theitem.postPhoto
    }
  },
  created() {
    this.getPostList()
    this.getlabelList()
  },
  methods: {
    getPostList() {
      getAction('/post/toCheckUserPost.do?userId=' + this.userId, {}).then(resp => {
        this.postList = resp.data
      })
    },
    commentDetails(item) {
      getAction('/post/commentDetails.do?postId=' + item.postId, {}).then(resp => {
        this.$refs.postDetails.commentList = resp.data
      })
      this.$refs.postDetails.dialogVisible = true
      this.$refs.postDetails.post = item
    },
    reset() {
      this.getPostList()
    },
    submitForm(theitem) {
      const selectedLabels = this.checkLabel.map(labelId => {
        const label = this.label.find(l => l.labelId === labelId)
        return label ? { labelId: label.labelId, labelName: label.labelName } : null
      }).filter(Boolean)
      this.theitem.labelList = selectedLabels
      postAction('/post/updateUserPost.do', this.theitem).then(resp => {
        if (resp.code === 200) {
          this.$message.success('修改成功!')
          this.dialogVisible = false
          this.getPostList()
        } else {
          this.$message.error(resp.msg)
        }
      })
    },
    getlabelList() {
      getAction('/post/getLabel.do').then(resp => {
        this.label = resp.data
      })
    },
    handleAvatarSuccess(res, file) {
      console.log('res:', res)
      console.log('file.raw:', file.raw) // 检查 file.raw 是否有效
      this.imageUrl = URL.createObjectURL(file.raw)
      this.file = file.raw
      this.theitem.postPhoto = res.data
    },
    beforeAvatarUpload(file) {
      const allowedTypes = ['image/jpeg', 'image/png', 'image/pdf', 'video/mp4']
      const isJPG = allowedTypes.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 20

      if (!isJPG) {
        this.$message.error('上传文件片格式有误!')
      }
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 20MB!')
      }
      return isJPG && isLt2M
    },
    isVideo(file) {
      if (!file) return false // 确保 file 不为空
      const ext = file.split('.').pop().toLowerCase()
      return ['mp4', 'avi', 'mov'].includes(ext)
    },
    handleCheckboxChange(item, isChecked) {
      const labelId = item.labelId
      if (isChecked) {
        this.checkLabel.push(labelId)
      } else {
        this.checkLabel = this.checkLabel.filter(id => id !== labelId)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.waterfall-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 10px;
}

:root {
  --main-border-color: #ccc;
  --margin-size: 10px;
}

.container {
  width: 300px;
  height: auto;
  border: 1px solid var(--main-border-color);
  padding: var(--margin-size);
  box-sizing: border-box;
}

.image-wrapper img {
  width: 300px;
  height: 300px;
  border-bottom: 1px solid var(--main-border-color);
  margin-bottom: var(--margin-size);
  border-radius: 5%;
}

.image-wrapper video {
  width: 300px;
  height: 300px;
  border-bottom: 1px solid var(--main-border-color);
  margin-bottom: var(--margin-size);
  border-radius: 5%;
}

.title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: var(--margin-size);
}

.user-info {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: var(--margin-size);
}

.user-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 5px;
}

.user-info span {
  font-size: 14px;
}

.checkbox-wrapper {
  position: relative;
  display: inline-block;
  margin-left: auto;
}

.checkbox-wrapper input[type="checkbox"] {
  display: none;
}

.heart-icon {
  cursor: pointer;
}

.like-count {
  font-size: 14px;
  color: #888;
  margin-left: 5px;
}
</style>
